package com.example.demo.service;

import com.example.demo.mapper.ContactMapper;
import com.example.demo.model.Contact;
import com.example.demo.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 意见建议相关业务代码
 * User: su
 * Date: 2023-04-08
 * Time: 14:34
 */
@Service
public class ContactService {
    // 注入 ContactMapper 依赖
    @Autowired
    private ContactMapper contactMapper;

    /**
     * 将意见建议添加到数据库中
     * @param name 名字
     * @param email 邮箱
     * @param subject 标题
     * @param message 内容
     * @return 返回 {code: 200/400, message: "成功"/"失败", data: true/false;}
     */
    public ResponseBodyMessage<Boolean> add(String name, String email, String subject, String message) {
        if (contactMapper.insertContact(name, email, subject, message) == 1) {
            return new ResponseBodyMessage<>(200,"添加意见反馈列表成功", true);
        } else {
            return new ResponseBodyMessage<>(400, "添加意见反馈列表失败", false);
        }
    }

    public ResponseBodyMessage<List<Contact>> getAll() {
        List<Contact> list=contactMapper.selectContact();
        if (list != null) {
            return new ResponseBodyMessage<>(200,"获取意见反馈列表成功",list);
        }
        else {
            return new ResponseBodyMessage<>(400, "获取意见反馈列表失败", null);
        }
    }

    public ResponseBodyMessage<List<Contact>> getPage(Integer pageIndex,Integer pageSize) {
        Integer offset = (pageIndex - 1) * pageSize;
        List<Contact> list=contactMapper.selectContactByPageIndex(pageSize,offset);
        if (list != null) {
            return new ResponseBodyMessage<>(200,"获取当前页意见反馈成功",list);
        }
        else {
            return new ResponseBodyMessage<>(400, "获取当前页意见反馈失败", null);
        }
    }

    public ResponseBodyMessage<Contact> get(Integer id) {
        Contact contact=contactMapper.selectContactById(id);
        if (contact != null) {
            return new ResponseBodyMessage<>(200,"获取意见反馈详情成功",contact);
        }
        else {
            return new ResponseBodyMessage<>(400, "获取意见反馈详情失败", null);
        }
    }

    public ResponseBodyMessage<Boolean> delete(Integer id) {
        if (contactMapper.deleteById(id) == 1) {
            return new ResponseBodyMessage<>(200,"删除意见反馈成功", true);
        } else {
            return new ResponseBodyMessage<>(400, "删除意反馈议失败", false);
        }
    }
}
